package org.hackystat.iw.projectoverviewer.page.newproject;

import static org.junit.Assert.assertTrue;
import java.io.File;
import org.apache.wicket.util.tester.FormTester;
import org.apache.wicket.util.tester.WicketTester;
import org.hackystat.iw.projectoverviewer.ProjectOverviewerApplication;
import org.hackystat.iw.projectoverviewer.page.projects.ProjectsPage;
import org.hackystat.iw.projectoverviewer.test.ProjectOverviewerTestHelper;
import org.hackystat.iw.projectoverviewer.xml.XmlConverter;
import org.junit.Test;

/**
 * Tests new project page.
 * 
 * @author Jianfei Liao
 * @author Nathan Britton
 */
public class TestNewProjectPage extends ProjectOverviewerTestHelper { //NOPMD
  /** String token for "new-project-form". */
  private static final String FORM_ID_TOKEN = "new-project-form";
  
  /**
   * Tests the new project creation.
   */
  @Test
  public void testNewProjectCreation() { // NOPMD
    ProjectOverviewerApplication testApp = new ProjectOverviewerApplication(getTestProperties());
    WicketTester tester = new WicketTester(testApp);
    
    tester.startPage(NewProjectPage.class);
    
    tester.assertRenderedPage(NewProjectPage.class);
    
    // try to create a new project with invalid information
    String testProjectName = "TestProject";
    String testOwner = "tester@hackystat.org";
    String testPassword = "testPass";
    String testUrl = "http://localhost/";
    FormTester newProjectForm = tester.newFormTester(FORM_ID_TOKEN);
    newProjectForm.setValue("projectName", testProjectName);
    newProjectForm.setValue("ownerName", testOwner);
    newProjectForm.setValue("password", testPassword);
    newProjectForm.setValue("svnUrl", testUrl);
    newProjectForm.submit(FORM_ID_TOKEN);
    
    // should stay in the same page
    tester.assertRenderedPage(NewProjectPage.class);
    
    // try to create a new project with NULL values
    testProjectName = null;
    testOwner = null;
    testPassword = null;
    testUrl = null;
    newProjectForm = tester.newFormTester(FORM_ID_TOKEN);
    newProjectForm.setValue("projectName", testProjectName);
    newProjectForm.setValue("ownerName", testOwner);
    newProjectForm.setValue("password", testPassword);
    newProjectForm.setValue("svnUrl", testUrl);
    newProjectForm.submit(FORM_ID_TOKEN);
    
    // should stay in the same page
    tester.assertRenderedPage(NewProjectPage.class);
    
    // create a new project with valid information
    testProjectName = "TestProject";
    testOwner = "admin@hackystat.org";
    testPassword = "admin@hackystat.org";
    testUrl = "http://informative-workspace.googlecode.com/svn/trunk/";
    newProjectForm = tester.newFormTester(FORM_ID_TOKEN);
    newProjectForm.setValue("projectName", testProjectName);
    newProjectForm.setValue("ownerName", testOwner);
    newProjectForm.setValue("password", testPassword);
    newProjectForm.setValue("svnUrl", testUrl);
    newProjectForm.submit(FORM_ID_TOKEN);
    
    // should go to the projects page
    tester.assertRenderedPage(ProjectsPage.class);
    
    // remove the test project
    ProjectOverviewerApplication app = (ProjectOverviewerApplication) tester.getApplication();
    assertTrue("Remove test project from Projects object", app.removeProject(testProjectName));
    
    String userHome = System.getProperty("user.home");
    File projectsFile = new File(userHome + "/.hackystat/projectoverviewer/projects.xml");
    String packageName = "org.hackystat.iw.projectoverviewer.jaxb.projects";
    assertTrue("Projects is removed from xml file", 
          XmlConverter.objectToXml(app.getProjects(), projectsFile, packageName));
  }
}
